export default class Toast {

    public static readonly CENTER = 0;
    public static readonly TOP = 1;
    public static readonly BOTTOM = 2;

    private _bgColor: cc.Color = cc.color(255, 255, 255, 180);
    private _textColor: cc.Color = cc.color(0, 0, 0, 255);


    private bgNode: cc.Node = null;
    private duration: number = null;
    private textNode: cc.Node = null;

    public static makeText(text: string, duration: number, node?: cc.Node, fontSize = 60): Toast {
        let toast = new Toast();
        toast.duration = duration;
        toast.init(text, node, fontSize);
        return toast;
    }

    public setBgColor(color: cc.Color) {
        this._bgColor = color;
        this.bgNode.color = this._bgColor;
        return this;
    }

    public setTextColor(color: cc.Color) {
        this._textColor = color;
        this.textNode.color = this._textColor;
        return this;
    }

    public setGravity(gravity: number) {
        if (gravity == Toast.CENTER) {
            this.bgNode.y = 0;
        } else if (gravity == Toast.TOP) {
            this.bgNode.y = this.bgNode.parent.height / 5 * 2;
        } else if (gravity == Toast.BOTTOM) {
            this.bgNode.y = -this.bgNode.parent.height / 5 * 2;
        }
        return this;
    }

    public show() {
        this.bgNode.opacity = 255;
        cc.tween(this.bgNode).by(this.duration, { x: 0, y: 0 }).to(this.duration, { opacity: 0 }).call((target: cc.Node) => {
            target.destroy();
        }).start();
        return this;
    }

    private init(text: string, node: cc.Node, fontSize = 60) {
        let parent = node || cc.find("Canvas");
        let width = parent.width;
        this.bgNode = new cc.Node();
        // Lable文本格式设置
        let textNode = new cc.Node();
        textNode.color = this._textColor;
        let textLabel = textNode.addComponent(cc.Label);
        textLabel.horizontalAlign = cc.Label.HorizontalAlign.CENTER;
        textLabel.verticalAlign = cc.Label.VerticalAlign.CENTER;
        textLabel.fontSize = fontSize;
        textLabel.string = text;
        // 当文本宽度过长时，设置为自动换行格式
        if (text.length * textLabel.fontSize > (width * 3) / 5) {
            textNode.width = (width * 3) / 5;
            textLabel.overflow = cc.Label.Overflow.RESIZE_HEIGHT;
        } else {
            textNode.width = text.length * textLabel.fontSize;
        }
        let lineCount = ~~((text.length * textLabel.fontSize) / ((width * 3) / 5)) + 1;
        textNode.height = textLabel.fontSize * lineCount;

        // 背景设置
        let ctx = this.bgNode.addComponent(cc.Graphics);
        ctx.arc(
            -textNode.width / 2,
            0,
            textNode.height / 2 + 20,
            0.5 * Math.PI,
            1.5 * Math.PI,
            true
        );
        ctx.lineTo(textNode.width / 2, -(textNode.height / 2 + 20));
        ctx.arc(
            textNode.width / 2,
            0,
            textNode.height / 2 + 20,
            1.5 * Math.PI,
            0.5 * Math.PI,
            true
        );
        ctx.lineTo(-textNode.width / 2, textNode.height / 2 + 20);
        ctx.fillColor = this._bgColor;
        ctx.fill();

        textNode.parent = this.bgNode;

        this.bgNode.opacity = 0;
        this.bgNode.parent = parent;
    }

}